<?php

/*
 * Created on 10/01/2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
class PlayerDao {

    function getBaseQuery() {
        return "select fk_team_id, player_id, roster_num, name, position, ma, st, ag, av, " .
        		"skills, injured, niggles, completions, touchdowns, interceptions, casualties, mvps, exps, " .
        		"cost, comments from players";
    }

    function loadPlayers($team) {
        $players = array ();

        $rs = executeQuery($this->getBaseQuery() . " where fk_team_id=$team->id and dead=0 order by roster_num");
        while ($row = mysql_fetch_row($rs)) {
            $player = new Player();
            $player->init($row);
            array_push($players, $player);
        }
        return $players;
    }

    function loadPlayer($playerId) {
        $rs = executeQuery($this->getBaseQuery() . " where player_id=$playerId");
        while ($row = mysql_fetch_row($rs)) {
            $player = new Player();
            $player->init($row);
            return $player;
        }
        return false;
    }

    function loadAllPlayers() {
        $players = array ();
        $rs = executeQuery($this->getBaseQuery() . " order by fk_team_id, roster_num, player_id");
        while ($row = mysql_fetch_row($rs)) {
            $player = new Player();
            $player->init($row);
            $players[$player->playerId] = $player;
        }
        return $players;
    }

    function findAllDeadPlayers() {
        $players = array ();
        $rs = executeQuery($this->getBaseQuery() . " where dead=1 order by fk_team_id, roster_num, player_id");
        while ($row = mysql_fetch_row($rs)) {
            $player = new Player();
            $player->init($row);
            $players[$player->playerId] = $player;
        }
        return $players;
    }

    function findTeamDeadPlayers($teamId) {
        $players = array ();
        $rs = executeQuery($this->getBaseQuery() . " where dead=1 and fk_team_id=$teamId order by fk_team_id, roster_num, player_id");
        while ($row = mysql_fetch_row($rs)) {
            $player = new Player();
            $player->init($row);
            $players[$player->playerId] = $player;
        }
        return $players;
    }
    
    /**
     * Permanently deletes a player's records
     */
    function delete($player) {
        executeQuery("delete from player_deltas where fk_player_id=$player->playerId");
        executeQuery("delete from players where player_id=$player->playerId");
    }
}
?>
